package com.example.my_music_palyer.common.tools;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 深度调试密码哈希问题的工具
 */
public class DebugHash {
    
    public static void main(String[] args) {
        System.out.println("=== 深度调试密码哈希问题 ===");
        
        // 创建编码器
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        
        // 测试密码
        String password = "123456";
        
        // 数据库中的哈希值
        String dbHash = "$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG";
        
        System.out.println("测试密码: " + password);
        System.out.println("数据库哈希: " + dbHash);
        System.out.println();
        
        // 1. 直接验证
        System.out.println("=== 直接验证 ===");
        boolean directMatch = encoder.matches(password, dbHash);
        System.out.println("直接验证结果: " + directMatch);
        
        // 2. 生成新哈希并验证
        System.out.println("\n=== 生成新哈希验证 ===");
        String newHash = encoder.encode(password);
        System.out.println("新生成哈希: " + newHash);
        boolean newMatch = encoder.matches(password, newHash);
        System.out.println("新哈希验证: " + newMatch);
        
        // 3. 测试不同的编码器实例
        System.out.println("\n=== 测试不同编码器实例 ===");
        BCryptPasswordEncoder encoder2 = new BCryptPasswordEncoder();
        boolean encoder2Match = encoder2.matches(password, dbHash);
        System.out.println("编码器2验证结果: " + encoder2Match);
        
        // 4. 测试不同的密码
        System.out.println("\n=== 测试不同密码 ===");
        String[] testPasswords = {
            "123456", "12345", "1234567", "admin", "password", 
            "123456 ", " 123456", "123456\n", "123456\r", "123456\t"
        };
        
        for (String testPwd : testPasswords) {
            boolean result = encoder.matches(testPwd, dbHash);
            System.out.println("密码 '" + testPwd + "' (长度:" + testPwd.length() + ") 验证结果: " + result);
        }
        
        // 5. 分析哈希值格式
        System.out.println("\n=== 哈希值格式分析 ===");
        System.out.println("哈希值长度: " + dbHash.length());
        System.out.println("是否以$2a$开头: " + dbHash.startsWith("$2a$"));
        System.out.println("是否以$2b$开头: " + dbHash.startsWith("$2b$"));
        System.out.println("是否以$2y$开头: " + dbHash.startsWith("$2y$"));
        
        // 6. 检查是否有隐藏字符
        System.out.println("\n=== 检查隐藏字符 ===");
        System.out.println("哈希值字节数组:");
        byte[] hashBytes = dbHash.getBytes();
        for (int i = 0; i < hashBytes.length; i++) {
            if (hashBytes[i] < 32 || hashBytes[i] > 126) {
                System.out.println("位置 " + i + ": 非ASCII字符 " + hashBytes[i]);
            }
        }
        
        // 7. 生成一个确定可用的哈希值
        System.out.println("\n=== 生成确定可用的哈希值 ===");
        String workingHash = encoder.encode(password);
        System.out.println("工作哈希值: " + workingHash);
        System.out.println("验证结果: " + encoder.matches(password, workingHash));
        
        // 8. SQL更新语句
        System.out.println("\n=== SQL更新语句 ===");
        System.out.println("UPDATE `user` SET `password` = '" + workingHash + "' WHERE `username` = 'admin';");
    }
}
